package edu.zut.cs.software.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

public class JdbcTest {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(JdbcTest.class.getName());

	String url = "jdbc:mysql://localhost:3306/example";
	String username = "student";
	String password = "st_123456";
	Connection con;

	@Test
	public void testConnect() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			this.con = DriverManager.getConnection(this.url, this.username, this.password);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("select name from t_student");
			while (rs.next()) {
				int numCount = rs.getMetaData().getColumnCount();
				StringBuffer buffer = new StringBuffer();
				buffer.append("\nStudent:{\n");
				for (int i = 1; i <= numCount; i++) {
					buffer.append("\t"+rs.getMetaData().getColumnLabel(i) + ":" + rs.getObject(i) + "\n");
				}
				buffer.append("}");
				if (logger.isInfoEnabled()) {
					logger.info("testConnect() - StringBuffer buffer={}", buffer); //$NON-NLS-1$
				}
			}
			rs.close();
			stmt.close();
			this.con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
